package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.enums.ImgRelationEnum;
import com.ruoyi.system.domain.enums.ImgTypeEnum;
import com.ruoyi.system.domain.equipment.BxwxDTO;
import com.ruoyi.system.domain.equipment.BxwxImg;
import com.ruoyi.system.domain.equipment.EquipmentBxwx;
import com.ruoyi.system.domain.equipment.EquipmentFaultType;
import com.ruoyi.system.mapper.equipment.BxwxImgMapper;
import com.ruoyi.system.mapper.equipment.EquipmentBookMapper;
import com.ruoyi.system.mapper.equipment.EquipmentFaultTypeMapper;
import com.ruoyi.system.service.IEquipmentFaultTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 故障知识库Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-19
 */
@Service
@RequiredArgsConstructor
public class EquipmentFaultTypeServiceImpl extends ServiceImpl<EquipmentFaultTypeMapper, EquipmentFaultType> implements IEquipmentFaultTypeService {
    private final BxwxImgMapper bxwxImgMapper;
    private final EquipmentFaultTypeMapper equipmentFaultTypeMapper;
    private final EquipmentBookMapper equipmentBookMapper;

    @Override
    public int add(EquipmentFaultType equipmentFaultType) {
        //生成故障编码
        String faultCode = genFaultCode();
        equipmentFaultType.setFaultCode(faultCode);
        equipmentFaultType.setCreateBy(SecurityUtils.getUsername());
        equipmentFaultType.setCreateTime(new Date());
        //设置设备信息
        BxwxDTO dto = equipmentBookMapper.selectEquipmentBookById(equipmentFaultType.getEquipmentId());
        equipmentFaultType.setEquipmentName(dto.getEquipmentName());
        equipmentFaultType.setEquipmentType(dto.getEquipmentType());

        baseMapper.insert(equipmentFaultType);
        //插入图片
        if (equipmentFaultType.getImgPath() != null && equipmentFaultType.getImgPath().size() > 0) {
            for (String path : equipmentFaultType.getImgPath()) {
                BxwxImg img = new BxwxImg();
                img.setImgPath(path);
                img.setRelationId(equipmentFaultType.getFaultTypeId());
                img.setRelationType(ImgRelationEnum.KNOWLEDGE.getValue());
                img.setType(ImgTypeEnum.WX.getValue());
                bxwxImgMapper.insert(img);
            }
        }
        return 1;
    }

    @Override
    public EquipmentFaultType getByIdWithImg(Long faultTypeId) {
        EquipmentFaultType equipmentFaultType = baseMapper.selectById(faultTypeId);
        List<BxwxImg> bxwxImgList = bxwxImgMapper.selectList(Wrappers.<BxwxImg>lambdaQuery()
                .select(BxwxImg::getImgPath)
                .eq(BxwxImg::getRelationId, faultTypeId)
                .eq(BxwxImg::getRelationType, ImgRelationEnum.KNOWLEDGE.getValue()));
        List<String> imgPath = bxwxImgList.stream().map(BxwxImg::getImgPath).collect(Collectors.toList());
        equipmentFaultType.setImgPath(imgPath);
        return equipmentFaultType;
    }

    /**
     * 生成故障编码
     * @return
     */
    private synchronized String genFaultCode() {
        String start="GZ";
        String mid = DateUtil.format(new Date(), "yyyyMMdd");
        String maxBxNo=equipmentFaultTypeMapper.selectFaultCode(start+mid+"%");
        if (maxBxNo!=null){
            int num = new Integer(maxBxNo.substring(10)) + 1;
            return start+mid+String.format("%04d",num);
        }
        return start+mid+"0001";
    }
}
